<?php
class ConsignmentAction extends CommonAction{
	public function index()
	{
		$this->display();
	}
	
	public function download()
	{
		$content = utf8ToGB("订单号,快递名称,物流单号" . "\n");
		$timestr = toDate(gmtTime(),"Y-m-d H:i:s");
		$content .= utf8ToGB("完整的订单号,圆通快递,ABC_12345\n");	
	    header("Content-Disposition: attachment; filename=cons_sample.csv");
	    echo $content; 
	}
	
	/*导入*/	
	public function importInsert()
	{
		set_time_limit(0);
		$gbExcel = $_FILES['excel'];		
		$csvPath = $this->getRealPath()."/admin/Runtime/Temp/".uniqid().".csv";
		$fail = 1;
		if(move_uploaded_file($gbExcel['tmp_name'],$csvPath))
		{
			$content = @file_get_contents($csvPath);
			$content = explode("\n",$content);
			unset($content[0]);
			foreach($content as $k=>$v)
			{
				if($v!='')
				{
					$imp_row = explode(",",$v);
					$order_info=M("Order")->where("sn='".trim($imp_row[0])."'")->find();
				
					if($order_info['money_status']==2)
					{
						$consignment_name=gbToUTF8($imp_row[1]);
						$delivery_code =  addslashes(htmlspecialchars(trim($imp_row[2])));
						$express_id = M('Express')->where("`name`='{$consignment_name}'")->max('id');
						
						if(M("OrderConsignment")->where("order_id='".intval($order_info['id'])."'")->count()==0)
						{
							//开始生成发货单
							$delivery_data = array();
							$delivery_data['order_id'] = $order_info['id'];
							$delivery_data['delivery_id'] = $order_info['delivery'];
							$delivery_data['delivery_code'] = $delivery_code;
							$delivery_data['delivery_fee'] = $order_info['delivery_fee'];
							$delivery_data['protect_fee'] = $order_info['protect_fee'];
							$delivery_data['protect'] = $order_info['protect'];
							$delivery_data['cost_calc'] = 1;
							$delivery_data['region_lv1'] = $order_info['region_lv1'];
							$delivery_data['region_lv2'] = $order_info['region_lv2'];
							$delivery_data['region_lv3'] = $order_info['region_lv3'];
							$delivery_data['region_lv4'] = $order_info['region_lv4'];
							$delivery_data['address'] = $order_info['address'];
							$delivery_data['mobile_phone'] = $order_info['mobile_phone'];
							$delivery_data['fix_phone'] = $order_info['fix_phone'];
							$delivery_data['consignee'] = $order_info['consignee'];
							$delivery_data['zip'] = $order_info['zip'];
							$delivery_data['email'] = $order_info['email'];
							$delivery_data['create_time'] = gmtTime();
							$delivery_data['express_id'] = $express_id;
							
							$consignment_id = M("OrderConsignment")->add($delivery_data);
							
							if(intval($consignment_id)>0)
							{
								$order_goods = M("OrderGoods")->where("order_id = ".$order_info['id'])->count();
								foreach($order_goods as $k=>$v)
								{
									$c_goods = array();
									$c_goods['order_goods_id'] = $v['id'];
									$c_goods['order_consignment_id'] = $consignment_id;
									$c_goods['number'] = $v['number'];
									M("OrderConsignmentGoods")->add($c_goods);
								}
							}
							M('Order')->where("id = ".$order_info['id'])->setField('goods_status',2);
						}
						else {
							$data['express_id'] =  $consignment_id;
							$data['delivery_code'] = $delivery_code;
							M("OrderConsignment")->where('order_id='.$order_info['id'])->save($data);
						}
						
						if(intval($_REQUEST['send_sms'])==1)
						{
							send_delivery_sms($consignment_id);
						}
						$fail = 0;
					}
				}
			}
		}
		@unlink($csvPath);
		
		if($fail==0)
			$this->success("批量发货完成".'<script language="JavaScript">var runtype = "ajaxSendRun"; user_id = 0; </script><script type="text/javascript" src="__TMPL__ThemeFiles/Js/ajaxRun.js"></script>');
		else
			$this->error("批量发货失败");
	}
}

?>